草庐IT

c++ - gcc 和 libstdc++ 向前兼容

全部标签

c++ - 为什么 C++17 destroy()/destroy_n() 函数向前运行(而不是向后运行)?

在C++17中,destroy()和destroy_n()运行一系列对象的析构函数。但是根据cppreference这是以前向方式完成的,即templatevoiddestroy(ForwardItfirst,ForwardItlast){for(;first!=last;++first)std::destroy_at(std::addressof(*first));}但是数组的销毁是reversetotheorderofconstruction按照标准要求。那么destroy()和destroy_n()的用例是什么?有没有和正常的数组销毁不一致的原因,或者这是cppreference

c++ - 关于 C++ 编译器的几个问题 : GCC, MSVC、Clang、Comeau 等

我有几个关于C++编译器的问题C++编译器是否需要one-passcompiler?标准是否在任何地方谈论它?特别是GCC是一次性编译器吗?如果是,那么为什么它会在thisexample中生成以下错误两次(尽管每个错误消息中的模板参数都不同)?error:declarationof‘adderitem’shadowsaparametererror:declarationof‘adderitem’shadowsaparameter一个更一般的问题one-passcompiler的优点和缺点是什么?和multi-passcompiler?有用的链接:AListofC/C++compiler

c++ - gcc -lpthread 不工作

我的系统中安装了ubuntu11。我有一个使用pthread库的c程序。我收到错误Undefinedreferencetosem_wait()即使我使用标志-lpthread编译。例如:gcc-lpthreadprog.c该程序在其他ubuntu安装上运行良好。 最佳答案 尝试:gcc-pthread而不是-lpthread。我相信,差异是显着的。后者链接到libpthread,前者链接到libpthread和其他一些东西!sem_wait是librt的一部分,因此您也可以使用gcc-lrt,但是-pthread会为您完成此操作(并

c++ - 如何修复 gcc -Wall "embedded '\0' in format"警告

这可能不是很重要,但我正在尝试修复g++提示的所有警告。在下面的代码中,我收到了snprintf()行的“embedded'\0'informat”警告。我该如何解决这个问题?intfilePathSize=path.size()+s.size()+1;charfilePath[filePathSize];snprintf(filePath,filePathSize,"%s%s\0",path.c_str(),s.c_str());提前致谢... 最佳答案 警告是有充分理由的:snprintf将认为\0标记字符串的结尾。如果您确实需

c++ - gcc编译C++代码: undefined reference to `operator new[](unsigned long long)'

有一段C++代码:#includeintmain(){intb=sizeof('a');if(b==4)printf("I'maCprogram!\n");elseprintf("I'maC++program!\n");}像这样编译:gccmain.cpp-omain它成功并给出:I'maC++program!然后在函数main的某处添加一行int*p1=newint[1000];它失败了:C:\Users\...\AppData\Local\Temp\cccJZ8kN.o:main1.cpp:(.text+0x1f):undefinedreferencetooperatornew[]

c++ - 如何在 gcc 中声明和定义一个纯函数?

GCC具有pure和const属性,其中const实际上用于真正的纯函数(pure用于idempotentfunctionswhicharealsoside-effectfree)。那么如何使用常量属性声明和定义函数呢?编辑:我对真正的纯函数感兴趣,那些用const属性声明的函数,而不是那些用pure属性声明的函数。 最佳答案 例子://Declaration:intsquare(intx)__attribute__((const));//Definition:int__attribute__((const))square(intx

c++ - 为什么 gcc 会在全局命名空间中隐藏重载函数?

voidf(){}namespacetest{voidf(int){}voidg(){f();}//erroringcc6.2.0}intmain(){test::g();}用g++-std=c++1zmain.cpp编译,输出如下:main.cpp:Infunction'voidtest::g()':main.cpp:9:4:error:toofewargumentstofunction'voidtest::f(int)'f();//erroringcc^main.cpp:5:6:note:declaredherevoidf(int){}我的编译器是gcc6.2.0。为什么gcc会在

c++ - 可能的模板和 constexpr——如果不兼容

我想在编译时计算e值(别担心,不是家庭作业),但出了点问题。template,size_tfactorial=1,size_tcount=1>constexprdoublee_impl(){ifconstexpr(limit==0){returnstatic_cast(result{}.num)/result{}.den;}returne_impl>,factorial*count,count+1>();}虽然计算值是正确的,但编译器会抛出有关模板溢出的错误。似乎limit变量超出了范围(低于0),但它不应该发生,因为0–情况正在被处理ifconstexpr(...)语句。所以问题是,

c++ - gcc 在这个概念定义中错误地评估了 std::declval 吗?

在这个概念定义中:#includetemplateconceptInvokable=requires(Funcf){{f(std::declval()...)}->Ret;};像这样实例化时:static_assert(Invokable);gcc-9.0.1(主干)转储(好吧,准确地说是标准库实现):$g++-O2-std=c++2a-fconcepts-Wall-Wextra-Werror-ctu1.cpperror:staticassertionfailed:declval()mustnotbeused!2204|static_assert(__declval_protector

c++ - avr-gcc:(看似)简单功能中不需要的序言/结尾

当尝试处理uint64中的单个字节时,AVRgcc⁽¹⁾给我一个奇怪的序言/结尾,而使用uint32_t编写的相同函数给了我一个单个ret(示例函数是NOP)。为什么gcc这样做?我该如何删除它?Youcanseethecodehere,inCompilerExplorer.⁽¹⁾来自Arduino1.8.9发行版的gcc5.4.0,参数=-O3-std=c++11。源代码:#includeuint32_tf_u32(uint32_tx){uniony{uint8_tp[4];uint32_tw;};returny{.p={y{.w=x}.p[0],y{.w=x}.p[1],y{.w=